
package com.kweative.model.test;

import com.kweative.model.data.Row;
import com.kweative.model.exception.StructureException;
import com.kweative.model.data.MySQL;
import com.kweative.model.data.DataProvider;
import com.kweative.model.query.JoinType;
import com.kweative.model.query.WhereStatement.Order;
import com.kweative.model.test.model.Article;
import com.kweative.model.test.model.BasicSchema;
import com.kweative.model.test.model.User;
import com.kweative.model.test.model.UserGroup;
import com.kweative.model.test.model.UserRow;
import java.sql.SQLException;
import junit.framework.TestCase;

/**
 *
 * @author henrik
 */
public class Basic extends TestCase {
    BasicSchema db;
    String q = "";

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        DataProvider.setProvider(new MySQL("devuser","devpass","devhost.dk"));
        db = new BasicSchema();
        
    }
    public void testNothing() throws SQLException, StructureException {
        //To check real speeds of subsequent tests
        db.update();
    }

    public void testBasicQuery() throws StructureException {
        header("BASIC");
        UserGroup u1 = db.UserGroup.as("U1");
        UserGroup u2 = db.UserGroup.as("U2");
        Article a1 = db.Article.as("A1");
        Article a2 = db.Article.as("A2");
        User U = db.User.as("U");
        
        q = U.select()
                    .columns(U.Email.as("UserEmail"),U.Password.as("UserPassword"))
                    .where(U.Email).eq("test@test.com")
                    .where(U.Email).eq("password")
                    .join(u1,JoinType.LEFT)
                        .where(u1.__primary).eq(U.__primary)
                        .where(u1.Name).isNotNull()
                        .columns(u1.Name)
                    .join(u2,JoinType.LEFT)
                        .where(u2.__primary).eq(U.__primary)
                        .where(u2.Name).isNotNull()
                        .end()
                    .join(a1,JoinType.LEFT)
                        .where(a1.__primary).eq(u1.__primary)
                        .where(a1.Title).isNotNull()
                        .end()
                    .join(a2,JoinType.LEFT)
                        .where(a2.__primary).eq(u2.__primary)
                        .where(a2.Title).isNotNull()
                        .end()
                    .limit(1,3)
                    .orderBy(U.Firstname, Order.DESCENDING)
                    .compile();

        log(q);

        q = db.User.select().row(1).compile();
        
        q = db.User.select()
                .using(db.User.UserPass,"test@test.dk","password")
                .compile();

        log(q);
    }
    public void testUsingJoins() {
        header("JOINS");

        q = db.User.select().using(db.UserGroup.Users).compile();

        q = db.User.select().using(db.UserGroup.Users).compile();

        log(q);
    }
    public void testDelete() {
        header("DELETE");

        q = db.User.delete().row(1).compile();

        q = db.User.delete()
                .using(db.User.UserPass,"test@test.dk","password")
                .compile();

        log(q);
    }
    public void testInsert() throws SQLException {
        header("INSERT");
        Row<User> userRow = new Row<User>(db.User);
        userRow.__primary = 2;

        q = db.Article.insert()
                    .set(db.Article.Title,"An article")
                    .set(db.Article.Body,"About all';TRUNCATE kinds of things")
                    .set(db.Article.User,userRow)
					.set(db.Article.Published,true)
                    .set(db.Article.Prio,10)
                    .compile();

        log(q);
    }
    public void testUpdate() {
        header("UPDATE");

        q = db.User.update()
                    .set(db.User.Email,"henrik@test.com")
                    .set(db.User.Password,"password")
                    .where(db.User.Email).isEqual("henrik@old.com")
                    .compile();
        log(q);
        UserRow userRow = new UserRow();

        db.Article.update(2).set(db.Article.User,userRow);


        Row<Article> articleRow = new Row<Article>(db.Article);
        articleRow.__primary = 5;
        q = db.User.update(2).add(db.Article.User,articleRow).compile();


        log(q);
    }
    public void testCreation() throws StructureException {
        header("CREATE");
        q = DataProvider.instance().serializeSchema(db);
        log(q);
    }
    private void header(String msg) {
        System.out.println("\n#### " + msg + " #####\n");
    }
    private void log(String msg) {
        System.out.println("SQL: " + msg);
    }
}
